package com.sctvcloud.dongqu.utils;

import android.content.Context;
import android.media.MediaMetadataRetriever;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;

import com.iceteck.silicompressorr.SiliCompressor;

import org.xutils.common.util.LogUtil;

import java.io.File;
import java.net.URISyntaxException;

/**
 * 视频压缩---如果自定义压缩宽高，视频会有翻转90度以及压缩失败的可能。
 */
public class VideoCompressAsyncTask extends AsyncTask<String, String, String> {
    private static final int BITRATE_2M = 2000 * 1000;
    private static final int BITRATE_4M = 4000 * 1000;//12000000,450000
    //压缩成功
    public static final int COMPRESS_SUCCESS = 0;
    //压缩失败
    public static final int COMPRESS_ERROR = 4;

    private boolean isCompressComplete = false;
    private String compressFailPath;
    private Context mContext;
    private Handler handler;

    public VideoCompressAsyncTask(Context context, Handler handler) {
        mContext = context;
        this.handler = handler;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        LogUtil.e("Silicompressor onPreExecute ");
    }

    @Override
    protected String doInBackground(String... paths) {
        String filePath = null;
        try {
            LogUtil.e("Silicompressor paths[0]: " + paths[0] + "   paths[1] = " + paths[1]);
            if (!TextUtils.isEmpty(paths[0])) {
                File oldFile = new File(paths[0]);
                long size = oldFile.length() / (1024 * 1024);
                int level = BITRATE_2M;
                if (size < 80) {
                    level = BITRATE_4M;
                }
                compressFailPath = paths[0];
                MediaMetadataRetriever retr = new MediaMetadataRetriever();
                retr.setDataSource(paths[0]);
                String height = retr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT); // 视频高度
                String width = retr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH); // 视频宽度
                String bit = retr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_BITRATE); // 视频bit
                String rotation = retr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION);
                int h = Integer.parseInt(height);
                int w = Integer.parseInt(width);
                int b = Integer.parseInt(bit);
                int r = Integer.parseInt(rotation);
                int xW = w;
                int xH = h;
                if (h > w) {
                    if (h >= 1280) {
                        xH = 1280;
                        xW = w * xH / h;
                    }
                } else {
                    if (w >= 1280) {
                        xW = 1280;
                        xH = h * xW / w;
                    }
                }
                LogUtil.e("Silicompressor height: " + height + "  width:" + width + " rotation:" + rotation + "  bit:" + bit + "---Build.VERSION.SDK_INT = " + Build.VERSION.SDK_INT);
                if ((r == 0 ||r == 90) && Build.VERSION.SDK_INT > 20) {
                    LogUtil.e("Silicompressor type:1 ");
                    filePath = SiliCompressor.with(mContext).compressVideo(paths[0], paths[1], xW, xH, level);
                } else {
                    LogUtil.e("Silicompressor type:2 ");
                    filePath = SiliCompressor.with(mContext).compressVideo(paths[0], paths[1], 0, 0, level);
                }
            } else {
                Message msg = new Message();
                msg.what = COMPRESS_ERROR;
                msg.obj = paths[0];
                handler.sendMessage(msg);
            }
        } catch (URISyntaxException e) {
            e.printStackTrace();
            LogUtil.e("Silicompressor URISyntaxException: " + e.getMessage());
            Message msg = new Message();
            msg.what = COMPRESS_ERROR;
            msg.obj = paths[0];
            handler.sendMessage(msg);
        }
        LogUtil.e("Silicompressor filePath: " + filePath);
        Message msg = new Message();
        msg.what = COMPRESS_SUCCESS;
        msg.obj = filePath;
        handler.sendMessage(msg);
        isCompressComplete = true;
        return filePath;

    }


    @Override
    protected void onPostExecute(String compressedFilePath) {
        super.onPostExecute(compressedFilePath);
        File imageFile = new File(compressedFilePath);
        float length = imageFile.length() / 1024f; // Size in KB
        String value;
        if (length >= 1024)
            value = length / 1024f + " MB";
        else
            value = length + " KB";
        LogUtil.e("Silicompressor length: " + value);
        MediaMetadataRetriever retr = new MediaMetadataRetriever();
        retr.setDataSource(compressedFilePath);
        String height = retr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT); // 视频高度
        String width = retr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH); // 视频宽度
        String bit = retr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_BITRATE); // 视频bit
        String rotation = retr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION);
        LogUtil.e("Silicompressor onPostExecute height: " + height + "  width" + width + " rotation:" + rotation + " bit " + bit);
        if (!isCompressComplete) {
            Message msg = new Message();
            msg.what = COMPRESS_ERROR;
            msg.obj = compressFailPath;
            handler.sendMessage(msg);
        }
//    }
    }

//    @Override
//    protected void onProgressUpdate(String... values) {
//        super.onProgressUpdate(values);
//        LogUtil.e("Silicompressor onProgressUpdate values: " + values  );
//        if(!isCompressComplete){
//            Message msg = new Message();
//            msg.what = COMPRESS_ERROR;
//            msg.obj = compressFailPath;
//            handler.sendMessage(msg);
//        }
//    }


}
